package com.tanhua.listener;

import com.tanhua.domain.mongo.PublishScore;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-publish",consumerGroup = "tanhua-publish-group")
public class MovementMQListener implements RocketMQListener<Map> {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void onMessage(Map message) {

        log.info("收到消息: message:{}",message);
        PublishScore publishScore = new PublishScore();
        publishScore.setPublishId(Long.parseLong(message.get("pid").toString()));
        publishScore.setUserId(Long.parseLong(message.get("userId").toString()));
        int type = (int) message.get("type");
        switch (type){
            case 1:
                publishScore.setScore(20D);
                break;
            case 2:
                publishScore.setScore(5D);
                break;
            case 3:
                publishScore.setScore(8D);
                break;
            case 4:
                publishScore.setScore(10D);
                break;
            case 5:
                publishScore.setScore(-5D);
                break;
            case 6:
                publishScore.setScore(-8D);
                break;
            default:
        }

        mongoTemplate.insert(publishScore);
    }
}
